package net.begincode.servlet.blog;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;

import com.google.gson.Gson;

import net.begincode.bean.BcBlog;
import net.begincode.bean.BcClassify;
import net.begincode.bean.BcUser;
import net.begincode.servlet.common.BaseServlet;

public class ClassifyServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(ClassifyServlet.class);
	/**
	 * 异步加载我的分类
	 * */
	public void selClassifyAjax(HttpServletRequest request, HttpServletResponse response)  {
		List<BcClassify> classifyList = new ArrayList<BcClassify>();
		try {
			String userId = request.getParameter("userId");
			if(userId != null && !userId.equals("")){
				classifyList = this.classifyService.selClassifyByUserId(userId);
			}else{
				Map  userMap = this.getCookieInfo(request, response);
				classifyList = this.classifyService.selClassifyByUserId(userMap.get("bcUserId").toString());
			}
			Gson gs = new Gson();
			response.getWriter().print(gs.toJson(classifyList));
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
		/**
		 * 保存分类信息
		 * */
		public void saveClassFyAjax(HttpServletRequest request, HttpServletResponse response) throws IOException{
        	Map  userMap = this.getCookieInfo(request, response);
            int bc_user_id = Integer.valueOf(userMap.get("bcUserId").toString());
            String classify_note = request.getParameter("classify_note");
            BcClassify bcClassify = new BcClassify();
            bcClassify.setBc_user_id(bc_user_id);
            bcClassify.setClassify_note(classify_note);
            bcClassify.setCheck_flag("0");
			String classfyId = request.getParameter("bc_classify_id");
			Map<String, Object> msgMap = new HashMap<String, Object>();
			if(classfyId.equals("")){
				//标识为空，说明是新增
				bcClassify = this.classifyService.addClassify(bcClassify);
	            if (bcClassify != null) {
	                msgMap.put("msg", "success");
                }else{
                    msgMap.put("msg", "fail");
                }
			}else{
				//标识不为空，说明是修改
				bcClassify.setBc_classify_id(Integer.parseInt(classfyId));
				int returnValue = this.classifyService.updClassify(bcClassify);
	            if (returnValue > 0) {
	                msgMap.put("msg", "success");
                }else{
                    msgMap.put("msg", "fail");
                }
			}
			msgMap.put("bcClassifys", bcClassify);
			Gson gs = new Gson();
			response.getWriter().print(gs.toJson(msgMap));
		}
	

       
       public void delClassifyAjax(HttpServletRequest request, HttpServletResponse response)  {
           try {
               int bc_classify_id = Integer.valueOf(request.getParameter("bc_classify_id"));
               int returnValue = this.classifyService.delClassify(bc_classify_id);
               Gson gs = new Gson();
               Map<String, Object> msgMap = new HashMap<String, Object>();
               if (returnValue > 0) {
                   msgMap.put("msg", "success");
                   msgMap.put("info", "删除成功");
               }else{
                   msgMap.put("msg", "fail");
                   msgMap.put("info", "删除失败");
               }
               response.getWriter().print(gs.toJson(msgMap));
               } catch (IOException e) {
                   e.printStackTrace();
               }
       }
}
